package com.cloudloan.common;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;

/**
 *配置文件加载
 */
public class DeployCommon {

    public static Map<String,Object> deploy(){
        Properties properties = new Properties();
        Map<String,Object> map=new HashMap<>();
        try {
            Resource resource = new ClassPathResource("Deploy/Deploy.properties");
            InputStream inputStream = resource.getInputStream();
            // 使用 UTF-8 编码加载 Properties 文件
            InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
            properties.load(reader);
        } catch (IOException e) {
            throw new RuntimeException("配置文件读取失败");
        }
        for (String propertyName : properties.stringPropertyNames()) {
            String[] values = properties.getProperty(propertyName).split(",");
            String formattedKey = convertToCamelCase(propertyName);
            map.put(formattedKey, values);
        }
        return map;
    }

    /**
     * 命名方式修改
     * @param propertyName
     * @return
     */
    private static String convertToCamelCase(String propertyName) {
        // 根据Java命名约定将属性名转换为驼峰命名
        StringBuilder camelCaseKey = new StringBuilder();
        boolean toUpperCase = false;
        for (char c : propertyName.toCharArray()) {
            if (c == '.') {
                toUpperCase = true;
            } else if (toUpperCase) {
                camelCaseKey.append(Character.toUpperCase(c));
                toUpperCase = false;
            } else {
                camelCaseKey.append(Character.toLowerCase(c));
            }
        }
        return camelCaseKey.toString();
    }

    /**
     * 根据名称获取数组值信息
     */
    public static String[] getName(String name){
        Properties properties = new Properties();
        try {
            Resource resource = new ClassPathResource("Deploy/Deploy.properties");
            InputStream inputStream = resource.getInputStream();
            // 使用 UTF-8 编码加载 Properties 文件
            InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
            properties.load(reader);
        } catch (IOException e) {
            throw new RuntimeException("配置文件读取失败");
        }
        String[] split = properties.getProperty(name).split(",");
        if (split==null&&split.length==0){
            throw new RuntimeException("未查询出"+name+"配置信息");
        }
        return split;
    }

    /**
     * 查看公共方法
     * @param name
     * @return
     */

    public static List<Map<String,Map<String,String>>> conmmon(String name) {
        Properties properties = new Properties();
        List<Map<String,Map<String,String>>> list=new ArrayList<>();
        try {
            Resource resource = new ClassPathResource("Deploy/Common.properties");
            InputStream inputStream = resource.getInputStream();
            // 使用 UTF-8 编码加载 Properties 文件
            InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
            properties.load(reader);
        } catch (IOException e) {
            throw new RuntimeException("配置文件读取失败");
        }
        Map<String, Map<String, String>> stringMapMap = convertPropertiesToMap(name, properties);
        list.add(stringMapMap);
        return list;

    }

    /**
     *
     * @param propertyName propertis中的键名称
     * @param properties  解析出来的properties对象
     * @return
     */
    private static Map<String, Map<String, String>> convertPropertiesToMap(String propertyName, Properties properties) {
        // 解析properties并转换为Map
        String propertyValue = properties.getProperty(propertyName);
        if (propertyValue != null) {
            Map<String, String> innerMap = Arrays.stream(propertyValue.split(","))
                    .map(entry -> entry.split(":"))
                    .filter(keyValue -> keyValue.length == 2)
                    .collect(Collectors.toMap(keyValue -> keyValue[0], keyValue -> keyValue[1]));

            Map<String, Map<String, String>> resultMap = new HashMap<>();
            String name = propertyName.replaceAll("\\.", "");
            resultMap.put(name, innerMap);
            return resultMap;
        }

        return null;
    }

}
